package com.xigege.netty.protocolDesignAndAnalysis.customProtocol.protocol;

import com.xigege.netty.protocolDesignAndAnalysis.customProtocol.message.LoginRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class TestMessageCodec {
    public static void main(String[] args) throws Exception {
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(
                // 处理粘包 半包现象
//                new LengthFieldBasedFrameDecoder(1024, 12, 4, 0, 0),
                new LoggingHandler(LogLevel.DEBUG),
                new MessageCodec()
        );

        // encode
        LoginRequestMessage loginRequestMessage = new LoginRequestMessage("wangmazi", "123456");
        // 出站
        embeddedChannel.writeOutbound(loginRequestMessage);

        // decode
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, loginRequestMessage, buffer);
        // 入站
        embeddedChannel.writeInbound(buffer);

//        //演示粘包 半包
//        ByteBuf s1 = buffer.slice(0, 100);
//        s1.retain();
//        ByteBuf s2 = buffer.slice(100, buffer.readableBytes() - 100);
//
//        embeddedChannel.writeInbound(s1);
//        embeddedChannel.writeInbound(s2);

    }
}
